package com.kim.smartring.adapter;

import java.util.ArrayList;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.kim.smartring.R;

/**
 * 设备列表适配器
 * 
 * @author Huang
 * 
 */
public class DevicesAdapter extends BaseAdapter {

	private ArrayList<BluetoothDevice> mDevices;
	private ArrayList<byte[]> mRecords;
	private ArrayList<Integer> mRSSIs;
	private LayoutInflater mInflater;

	public DevicesAdapter(Context context) {
		super();
		mDevices = new ArrayList<BluetoothDevice>();
		mRecords = new ArrayList<byte[]>();
		mRSSIs = new ArrayList<Integer>();
		mInflater = LayoutInflater.from(context);
	}

	public void addDevice(BluetoothDevice device, int rssi, byte[] scanRecord) {
		if (mDevices.contains(device) == false) {
			mDevices.add(device);
			mRSSIs.add(rssi);
			mRecords.add(scanRecord);
		}
	}

	public BluetoothDevice getDevice(int index) {
		return mDevices.get(index);
	}

	public int getRssi(int index) {
		return mRSSIs.get(index);
	}

	public void clearList() {
		mDevices.clear();
		mRSSIs.clear();
		mRecords.clear();
	}

	@Override
	public int getCount() {
		return mDevices.size();
	}

	@Override
	public Object getItem(int position) {
		return getDevice(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// get already available view or create new if necessary
		ViewHolder fields;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.item_device, null);
			fields = new ViewHolder();
			fields.deviceAddress = (TextView) convertView
					.findViewById(R.id.deviceAddress);
			fields.deviceName = (TextView) convertView
					.findViewById(R.id.deviceName);
			fields.deviceRssi = (TextView) convertView
					.findViewById(R.id.deviceRssi);
			convertView.setTag(fields);
		} else {
			fields = (ViewHolder) convertView.getTag();
		}

		// set proper values into the view
		BluetoothDevice device = mDevices.get(position);
		int rssi = mRSSIs.get(position);
		String rssiString = (rssi == 0) ? "N/A" : rssi + " db";
		String name = device.getName();
		String address = device.getAddress();
		if (name == null || name.length() <= 0)
			name = "Unknown Device";

		fields.deviceName.setText(name);
		fields.deviceAddress.setText(address);
		fields.deviceRssi.setText(rssiString);

		return convertView;
	}

	private class ViewHolder {
		TextView deviceName;
		TextView deviceAddress;
		TextView deviceRssi;
	}

}
